package cn.rkylin.oms.goods.util.handler.impl;

import java.util.List;
import java.util.Map;

import cn.rkylin.oms.goods.util.handler.SnOpenableValidateHandler;
import cn.rkylin.oms.goods.util.request.Request;
import cn.rkylin.oms.warehouse.stockGoods.vo.StockGoodsVO;

/**
 * 类名: StockValidHandler <br/>
 * 方法名: 库存相关校验类 <br/>
 * date: 2018年6月28日 下午2:13:31 <br/>
 *
 * @author zhanghao
 * @version 
 */
public class StockValidHandler extends SnOpenableValidateHandler{
    /**
     * 校验request中的商品是否有库存-true：有库存；false：没有库存.
     * @throws Exception 
     * @see cn.rkylin.oms.goods.util.handler.SnOpenableValidateHandler#isValid(cn.rkylin.oms.goods.util.request.Request)
     */
    @Override
    public boolean isValid(Request request) throws Exception {
        StockGoodsVO queryParamVO = new StockGoodsVO();
        queryParamVO.setPrjId(request.getGoodsInfo().getPrjId());
        queryParamVO.setEntId(request.getGoodsInfo().getEntId());
        queryParamVO.setGoodsId(request.getGoodsInfo().getGoodsId());
        /*
         * 对商品的第三方库存而言，如果存在库存数大于0，则校验失败
         */
        List<StockGoodsVO> extStorInfoList = stockGoodsService.getStockGoodsExtStorInfo(queryParamVO);
        for (StockGoodsVO stockGoodsVO : extStorInfoList) {
            if(stockGoodsVO.getTotalCount()>0) {
                return false;
            }
        }
        
        /*
         * 对商品的本地库存而言，如果库存数大于0或者残次品数大于0，则校验失败
         */
        List<StockGoodsVO> localStorInfoList = stockGoodsService.getStockGoodsLocaStorInfo(queryParamVO);
        for (StockGoodsVO stockGoodsVO : localStorInfoList) {
            if(stockGoodsVO.getStockQty()>0||stockGoodsVO.getJunkQty()>0) {
                return false;
            }
        }
        
        return true;
    }

    @Override
    public String getMessage() {
        return "该系统商品当前库存不为0，不允许变更 ";
    }

    @Override
    public int getUnGoingCount(Map<String, String> paramMap) throws Exception {
        return 0;
    }

}
